package com.gisocn.onemap.utils;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.Excluder;
import com.google.gson.internal.bind.MyReflectiveTypeAdapterFactory;
import com.google.gson.internal.bind.TypeAdapters;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/* loaded from: classes2.dex */
public class GsonUtil {
    private static TypeAdapter<Number> INTEGER = new TypeAdapter<Number>() { // from class: com.gisocn.onemap.utils.GsonUtil.3
        /* JADX WARN: Can't rename method to resolve collision */
        @Override // com.google.gson.TypeAdapter
        public Number read(JsonReader jsonReader) throws IOException {
            if (jsonReader.peek() == JsonToken.NULL) {
                jsonReader.nextNull();
                return null;
            }
            try {
                return Integer.valueOf(jsonReader.nextInt());
            } catch (NumberFormatException unused) {
                jsonReader.nextString();
                return 0;
            }
        }

        @Override // com.google.gson.TypeAdapter
        public void write(JsonWriter jsonWriter, Number number) throws IOException {
            jsonWriter.value(number);
        }
    };
    private static TypeAdapter<Number> LONG = new TypeAdapter<Number>() { // from class: com.gisocn.onemap.utils.GsonUtil.4
        /* JADX WARN: Can't rename method to resolve collision */
        @Override // com.google.gson.TypeAdapter
        public Number read(JsonReader jsonReader) throws IOException {
            if (jsonReader.peek() == JsonToken.NULL) {
                jsonReader.nextNull();
                return null;
            }
            try {
                return Long.valueOf(jsonReader.nextLong());
            } catch (Exception unused) {
                jsonReader.nextString();
                return 0;
            }
        }

        @Override // com.google.gson.TypeAdapter
        public void write(JsonWriter jsonWriter, Number number) throws IOException {
            jsonWriter.value(number);
        }
    };
    private static volatile GsonUtil sInstance;
    private Gson gson;

    private GsonUtil() {
    }

    public static GsonUtil getInstance() {
        if (sInstance == null) {
            synchronized (GsonUtil.class) {
                if (sInstance == null) {
                    sInstance = new GsonUtil();
                }
            }
        }
        return sInstance;
    }

    public Gson getGson() {
        if (this.gson == null) {
            this.gson = buildGson();
        }
        return this.gson;
    }

    private Gson buildGson() {
        Gson create = new GsonBuilder().registerTypeAdapterFactory(TypeAdapters.newFactory(Integer.TYPE, Integer.class, INTEGER)).registerTypeAdapterFactory(TypeAdapters.newFactory(Long.TYPE, Long.class, LONG)).registerTypeHierarchyAdapter(List.class, new JsonDeserializer<List<?>>() { // from class: com.gisocn.onemap.utils.GsonUtil.2
            @Override // com.google.gson.JsonDeserializer
            public List<?> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                if (jsonElement.isJsonArray()) {
                    JsonArray asJsonArray = jsonElement.getAsJsonArray();
                    Type type2 = ((ParameterizedType) type).getActualTypeArguments()[0];
                    ArrayList arrayList = new ArrayList();
                    for (int i = 0; i < asJsonArray.size(); i++) {
                        arrayList.add(jsonDeserializationContext.deserialize(asJsonArray.get(i), type2));
                    }
                    return arrayList;
                }
                return Collections.EMPTY_LIST;
            }
        }).registerTypeHierarchyAdapter(String.class, new JsonDeserializer<String>() { // from class: com.gisocn.onemap.utils.GsonUtil.1
            @Override // com.google.gson.JsonDeserializer
            public String deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                return (jsonElement.isJsonNull() || jsonElement.isJsonObject()) ? "" : jsonElement.getAsString();
            }
        }).create();
        try {
            Field declaredField = create.getClass().getDeclaredField("constructorConstructor");
            declaredField.setAccessible(true);
            Field declaredField2 = create.getClass().getDeclaredField("factories");
            declaredField2.setAccessible(true);
            List list = (List) declaredField2.get(create);
            ArrayList arrayList = new ArrayList(list);
            arrayList.remove(list.size() - 1);
            arrayList.add(new MyReflectiveTypeAdapterFactory((ConstructorConstructor) declaredField.get(create), FieldNamingPolicy.IDENTITY, Excluder.DEFAULT));
            declaredField2.set(create, Collections.unmodifiableList(arrayList));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e2) {
            e2.printStackTrace();
        }
        return create;
    }
}
